package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.entity.HouseHoldinfos;
import com.woniu.entity.Sales;
import com.woniu.mapper.SalesMapper;
import com.woniu.service.IHouseHoldinfosService;
import com.woniu.service.ISalesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 * @author Gewang
 * @since 2021-04-01
 */
@Transactional
@Service
class SalesServiceImpl extends ServiceImpl<SalesMapper, Sales> implements ISalesService {
    @Autowired
    private SalesMapper salesMapper;
    @Autowired
    private IHouseHoldinfosService iHouseHoldinfosService;

    @Override
    public List<Sales> findByCid(Integer cid) {
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.eq("State",3);
        queryWrapper.eq("Customer_ID",cid);
        List list = salesMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<Sales> findByCustomerId(Integer cid) {
        QueryWrapper queryWrapper = new QueryWrapper();

        queryWrapper.eq("Customer_ID", cid);
        List list = salesMapper.selectList(queryWrapper);
        return list;
    }

    /**
     * 根据状态查
     *
     * @param state
     * @return
     */
    @Override
    public List<Sales> findByState(Integer state) {
        List<Sales> byState = salesMapper.findByState(state);
        return byState;
    }

    @Override
    public Sales findById(Integer sid) {
        Sales sales = salesMapper.selectById(sid);
        return sales;
    }

    @Override
    public int updateState(Integer sid, Integer state) {
        //根据sid 查找户详细信息表id
        Sales sales = salesMapper.findBySid(sid);
        int houseHoldInfoID = sales.getHouseholdinfoId();
        //修改户状态
        HouseHoldinfos houseHoldinfos = new HouseHoldinfos();
        houseHoldinfos.setState(state);
        houseHoldinfos.setHouseholdinfoId(houseHoldInfoID);
        ResultVO resultVO = iHouseHoldinfosService.updateByID(houseHoldinfos);
        System.out.println(resultVO);
        //修改销售表状态
        Sales sales1 = new Sales();
        sales1.setSalesId(sid);
        sales1.setState(state);
        int i = salesMapper.updateById(sales1);
        return i;
    }

    @Override
    public int delete(Integer sid) {
        //根据sid 查找户详细信息表id
        Sales sales = salesMapper.findBySid(sid);
        System.out.println(sales);
        int houseHoldInfoID = sales.getHouseholdinfoId();
        //修改户状态
        HouseHoldinfos houseHoldinfos = new HouseHoldinfos();
        houseHoldinfos.setState(1);
        houseHoldinfos.setHouseholdinfoId(houseHoldInfoID);
        iHouseHoldinfosService.updateByID(houseHoldinfos);
        //删除销售表
        int i = salesMapper.deleteById(sid);
        return i;
    }

    @Override
    public int add(Sales sales) {
        //新增销售表
        int insert = salesMapper.insert(sales);
        //将对应户信息Id 状态改为3
        Integer householdinfoId = sales.getHouseholdinfoId();
        HouseHoldinfos houseHoldinfos = new HouseHoldinfos();
        houseHoldinfos.setState(3);
        houseHoldinfos.setHouseholdinfoId(householdinfoId);
        iHouseHoldinfosService.updateByID(houseHoldinfos);
        return insert;
    }

    @Override
    public List<Sales> find() {
        List<Sales> sales = salesMapper.find();
        return sales;
    }

    @Override
    public List<Sales> findByName(String name) {
        List<Sales> byName = salesMapper.findByName(name);
        return byName;
    }
}